package cn.edu.ujn.park.controller;

import cn.edu.ujn.park.dao.User;
import cn.edu.ujn.park.dao.Vehicle;
import cn.edu.ujn.park.service.IVehicleService;
import cn.edu.ujn.park.service.VehicleServiceImpl;
import cn.edu.ujn.util.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class VehicleController {
    @Autowired
    private IVehicleService iVehicleService;
   @GetMapping("/vehicle")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, @ModelAttribute Vehicle vehicle){
       vehicle.setStart((page-1)*rows);
        vehicle.setRows(rows);
        if("".equals(vehicle.getVehicleId())){
            vehicle.setVehicleId(null);
        }
        if("".equals(vehicle.getUserId())){
           vehicle.setUserId(null);
        }
        if("".equals(vehicle.getLicensePlateNumber())){
           vehicle.setLicensePlateNumber(null);
        }
        if("".equals(vehicle.getVehicleType())){
           vehicle.setVehicleType(null);
        }
        Page<Vehicle> page1 = iVehicleService.selectAll(vehicle);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "vehicle_manage";
    }
    @GetMapping("/vehicleself")
    public String list2(Model model, @RequestParam(value = "page",defaultValue = "1")Integer page ,
                        @RequestParam(value = "row",defaultValue = "10") Integer rows, @ModelAttribute Vehicle vehicle, HttpServletRequest request){
        vehicle.setStart((page-1)*rows);
        vehicle.setRows(rows);
        Integer vehicleId = (Integer) request.getSession().getAttribute("vehicleId");
        vehicle.setVehicleId(vehicleId);
        Page<Vehicle> page1 = iVehicleService.selectAll(vehicle);
        page1.setPage(page);
        model.addAttribute("page",page1);
        return "vehicle_user";
    }
@PostMapping("/vehicle")
@ResponseBody
public String createVehicle(Vehicle vehicle){

    iVehicleService.insert(vehicle);

    return "OK";
}

    /**
     * 根据id 获取Vehicle对象
     * @param id
     * @return
     */
    @GetMapping("/vehicle/{id}")
    @ResponseBody
    public Vehicle editVehicle(@PathVariable("id") Integer id){
        Vehicle vehicle = iVehicleService.getVehicleById(id);
        return vehicle;
    }

    /**
     * 更新user
     * @param vehicle
     * @return
     */
    @PutMapping("/vehicle")
    @ResponseBody
    public String updateVehicle(@RequestBody Vehicle vehicle){

        iVehicleService.updateByPrimaryKey(vehicle);

        return "OK";
    }

    /**
     * 删除user
     * @param id
     * @return
     */

    @DeleteMapping("/vehicle/{id}")
    @ResponseBody
    public String deleteVehicle(@PathVariable("id") Integer id){

        iVehicleService.deleteByPrimaryKey(id);
        return "OK";
    }

}
